﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;
namespace DAO
{
    public class PhieuThuDAO
    {
        #region 1. Them
        public static bool ThemPhieuThuTien(PhieuThuDTO ptt)
        {
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaPhieuThu", ptt.MaPhieuThu));
                sqlParams.Add(new SqlParameter("@SoThe", ptt.SoThe));
                sqlParams.Add(new SqlParameter("@SoTienThu", ptt.SoTienThu));
                sqlParams.Add(new SqlParameter("@NgayThu", ptt.NgayThu));

                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_InsertPHIEUTHUTIEN", sqlParams);
                if (n == 1)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
        #endregion
        #region 2. Sua
        public static bool SuaPhieuThuTien(PhieuThuDTO ptt)
        {
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaPhieuThu", ptt.MaPhieuThu));
                sqlParams.Add(new SqlParameter("@SoThe", ptt.SoThe));
                sqlParams.Add(new SqlParameter("@SoTienThu", ptt.SoTienThu));
                sqlParams.Add(new SqlParameter("@NgayThu", ptt.NgayThu));

                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_UpdatePHIEUTHUTIEN", sqlParams);
                if (n == 1)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
        #endregion
        #region 3. Xoa
        public static bool XoaPhieuThuTien(String maptt){
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaPhieuThu", maptt));

                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_DeletePHIEUTHUTIEN", sqlParams);
                if (n == 1)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
        #endregion
        #region 4. Tim kiem
        public static PhieuThuDTO TimPhieuThuTien(String maptt){
            PhieuThuDTO result = new PhieuThuDTO();
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaPhieuThu", maptt));

                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectPHIEUTHUTIEN", sqlParams);
                DataRow dr = dt.Rows[0];

                result.MaPhieuThu = dr["MaPhieuThu"].ToString();
                result.SoThe = dr["SoThe"].ToString();
                result.SoTienThu = decimal.Parse(dr["SoTienThu"].ToString());
                result.NgayThu = DateTime.Parse(dr["NgayThu"].ToString());
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return result;
        }
        public static List<PhieuThuDTO> TimTatCaPhieuThu(){
            List<PhieuThuDTO> result = new List<PhieuThuDTO>();
            try
            {
                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectPHIEUTHUTIENsAll");
                foreach (DataRow dr in dt.Rows)
                {
                    PhieuThuDTO tmp = new PhieuThuDTO();

                    tmp.MaPhieuThu = dr["MaPhieuThu"].ToString();
                    tmp.SoThe = dr["SoThe"].ToString();
                    tmp.SoTienThu = decimal.Parse(dr["SoTienThu"].ToString());
                    tmp.NgayThu = DateTime.Parse(dr["NgayThu"].ToString());

                    result.Add(tmp);
                }
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return result;
        }
        // public static List<PhieuThuDTO> TimPhieuThuTheoNgay(DateTime ngaythu)
        // public static List<PhieuThuDTO> TimPhieuThuTheoSoThe(String mathe)
        public static bool CheckPhieuThuTienByID(String maptt){
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaPhieuThu", maptt));

                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectPHIEUTHUTIEN", sqlParams);
                if (dt.Rows.Count > 0)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return result;
        }
        //View table
        public static DataTable ViewPhieuThu()
        {
            DataTable dt= new DataTable();
            try
            {
                dt = SqlDataAccessHelper.ExecuteQuery("usp_ViewPhieuThu");
            }
            catch(Exception ex)
            {

            }
            return dt;
        }

        #endregion

    }
}
